package com.medical.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.medical.admin.constant.Constant;
import com.medical.admin.domain.Consult;
import com.medical.admin.service.ConsultService;
import com.medical.common.core.domain.Result;
import com.medical.common.core.utils.StringUtils;
import com.medical.common.core.utils.TimeUtils;
import com.medical.common.core.web.controller.BaseController;
import com.medical.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: Medical
 * @BelongsPackage: com.medical.manage.controller
 * @Author: LX
 * @CreateTime: 2025-03-01  01:43
 * @Description: TODO
 * @Version: 1.0
 */

@RestController
@RequestMapping("consult")
public class ConsultController extends BaseController {

    @Autowired
    private ConsultService consultService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 咨询列表
     * @return
     */
    @GetMapping("/list")
    public Result<TableDataInfo> list() {
        //启动分页
        startPage();
        String s = redisTemplate.opsForValue().get(Constant.SYNC_CONSULT_LIST);
        if (StringUtils.isNotBlank(s)){
            List<Consult> consults = JSONObject.parseArray(s, Consult.class);
            consults.forEach(item -> item.setReleaseTime(TimeUtils.getRelativeTime(item.getCreateTime())));
            return getDataTable(consults);
        }
        List<Consult> list = consultService.list();
        list.forEach(item -> item.setReleaseTime(TimeUtils.getRelativeTime(item.getCreateTime())));
        return getDataTable(list);
    }


    /**
     * 咨询详情页
     * @param consultId
     * @return
     */
    @GetMapping("find/{consultId}")
    public Consult find(@PathVariable("consultId") Integer consultId) {
        LambdaQueryWrapper<Consult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Consult::getConsultId, consultId);
        return consultService.getOne(wrapper);
    }
}
